# -*- coding: utf-8 -*-
# @project: huawang-api20230914
# @file： ProductManagrmentpage.py
# @Time：2023/8/14 17:14
# @Author：liaogan
from selenium.webdriver.common.by import By

from common.commin.basePage import BasePage
from common.commin.commin import object_left_click, object_send_keys, elements_visibility, elements_clickable, \
    get_element_text, get_ele_text, get_elelment, click_element, input_text, get_elements_text
from data.randomly_generated_data import generate_random_intro, generate_random_unit
from data.yk_names import Spel, Pubelement, Product_search
from po.jinhua.ht.jinhua_ht_main.jinhua_base import JHBase
from common.log.log import log
from time import sleep
import allure


# 商品管理-菜单页页
class CommdityManagrmentpage(JHBase):
    """添加商品类"""

    @allure.step('商品搜索')
    def product_search(self, searchname):

        try:
            log.info('商品管理：输入商品信息，进行搜索商品')
            input_text(self.driver, Product_search.el_input_goods(), searchname,desc="输入商品名称")  # 输入商品名称
            click_element(self.driver, Product_search.el_Search_tpye())  # 点击搜索类型
            click_element(self.driver, Product_search.el_dowm_goods_name())  # 选择商品名称
            click_element(self.driver, Product_search.el_button_search())  # 点击搜索
            sleep(2)
            return CommdityManagrmentpage(self.driver)
        except Exception as e:
            log.error(f'搜索商品出现错误，异常为{e}')
            raise e

    @allure.step('获取第一页所有上架商品')
    def get_search_product_names(self):

        click_element(self.driver, Product_search.el_Pending_Product(), desc="点击出售中产品")
        sleep(1)
        return get_elements_text(self.driver, Product_search.el_up_allproduct(), desc='搜索第一页所有上架商品')

    @allure.step('获取搜索到的第一个商品')
    def get_first_productname(self):
        return get_element_text(self.driver, Spel.el_up_first_product(), desc='自动化0825')

    @allure.step('获取搜索未上架的第一个商品')
    # 未上架商品
    def get_first_down_new_productname(self):
        return get_element_text(self.driver, Spel.el_down_first_product(), desc='自动化0828')

    @allure.step('获取搜索到的第一个商品')
    def get_first_proname(self):
        return get_ele_text(self.driver, Spel.el_up_first_product())

    # 添加商品
    @allure.step('新增商品，发布商品操作不上架')
    def add_product(self, input_productname):
        try:
            log.info('新增商品，发布商品操作不上架')
            # 商品新增按钮
            object_left_click(self.driver, Pubelement.el_Product_add())
            sleep(2)

            self.driver.execute_script("window.scrollTo(0,0);")  # 将浏览器滑动条移动到顶部
            # object_left_click(self.driver,Spel.el_button_product_select())#点击下菜单
            # object_left_click(self.driver,Spel.el_product_1())#选择普通商品
            # 输入商品名称
            object_send_keys(self.driver, Spel.el_input_product_name(), input_productname)  # 输入商品名称
            object_left_click(self.driver, Spel.el_product_type_button())
            object_left_click(self.driver, Spel.el_product_type_name())  # 选择酱酒
            object_send_keys(self.driver, Spel.el_product_keyword(), "嘛嘛哒哒2023")  # 关键字
            object_send_keys(self.driver, Spel.el_product_unit(), generate_random_unit())  # 单位
            object_send_keys(self.driver, Spel.el_product_introduction(), generate_random_intro())  # 简介
            object_left_click(self.driver, Spel.el_tax_code_button())  # 点击税收编码
            object_left_click(self.driver, Spel.el_tax_code_select())  # 选择果酒
            sleep(1)
            """商品封面图"""
            object_left_click(self.driver, Spel.el_product_Background())
            sleep(1)
            # object_left_click(self.driver,Spel.el_product_select_Background())
            object_left_click(self.driver, Spel.el_product_frist())
            object_left_click(self.driver, Spel.el_button_Background())
            sleep(1)
            self.driver.execute_script('window.scrollBy(0,300)')  # 拖动下啦菜单
            """商品播图"""
            object_left_click(self.driver, Spel.el_product_banner())
            sleep(2)
            # object_left_click(self.driver, Spel.el_product_select_banner_Background())
            object_left_click(self.driver, Spel.el_banner_product_frist())
            object_left_click(self.driver, Spel.el_banner_button_Background())
            sleep(1)
            self.driver.execute_script('window.scrollBy(0,300)')
            # """商品规格"""
            # object_left_click(self.driver,Spel.el_product_specifications1())#单规格
            object_send_keys(self.driver, Spel.el_input_sale(), 5)  # 售价
            object_send_keys(self.driver, Spel.el_input_costprice(), 3)  # 成本价
            object_send_keys(self.driver, Spel.el_input_originalprice(), 5)  # 原价
            object_send_keys(self.driver, Spel.el_input_invent(), 99)  # 库存
            object_send_keys(self.driver, Spel.el_input_num(), 1)  # 数量
            object_send_keys(self.driver, Spel.el_input_productnum(), 12345678)  # 商品编码
            object_send_keys(self.driver, Spel.el_input_weight(), 1)  # 重量
            object_send_keys(self.driver, Spel.el_input_volume(), 1)  # 体积
            sleep(2)
            """运费模版"""
            object_left_click(self.driver, Spel.el_button_Freight_template())  # 点击运费模版
            object_left_click(self.driver, Spel.el_select_Freight_template())  # 选择包邮
            object_send_keys(self.driver, Spel.el_input_product_details(), 'test2023')  # 输入商品详情内容
            # 2250移动到最低部。
            self.driver.execute_script('window.scrollBy(0,1000)')
            object_send_keys(self.driver, Spel.el_input_virtuallysale(), 999)  # 虚拟销量
            object_send_keys(self.driver, Spel.el_input_sort(), 1)  # 排序
            click_element(self.driver, Spel.el_delist())  # 商品下架
            object_left_click(self.driver, Spel.el_button_sava())  # 点击保存
            sleep(2)
            log.info('添加商品完成！')
            # click_element(self.driver, Spel.el_down_product())  # 点击待上架商品
        except Exception as e:
            log.error(f'添加商品出现错误，异常为{e}')

    @allure.step('搜索添加的商品')
    def SearchAddProduct(self, input_productname):
        log.info('搜索添加的商品')
        click_element(self.driver, Spel.el_down_product())  # 点击待上架商品
        input_text(self.driver, Product_search.el_dwon_InoutGoods(), input_productname)  # 输入商品内容
        click_element(self.driver, Product_search.el_down_Search_product())  # 选择搜索类型
        sleep(1)
        click_element(self.driver, Product_search.el_down_ProductName())  # 点击商品名称确认
        click_element(self.driver, Product_search.el_dwon_Search())  # 点击商品搜索
        sleep(1)

        return get_element_text(self.driver, Spel.el_down_first_product(), desc='自动化0828')

    @allure.step('删除待上架第一个商品')
    def delete_product(self):
        try:
            log.info("删除待上架商品操作")
            # click_element(self.driver, Spel.el_search_product())
            click_element(self.driver, Spel.el_delete_down_first_product())
            click_element(self.driver, Spel.el_frist_button())
            sleep(1)
            # tips=get_element_text(self.driver, Spel.el_delete_uess(), desc='自动化0828')
            # log.info(f'获取tips提示文案为：{tips}')
            # if tips == "删除成功":
            #     log.info('商品删除成功')
            return CommdityManagrmentpage(self.driver)
        except Exception as e:
            log.error(f"删除商品出现错误，异常为{e}")

    @allure.step('获取删除商品tips提示文案')
    def get_yk_tips(self):
        try:
            tips = get_element_text(self.driver, Spel.el_delete_uess(), desc='自动化0828')
            log.info(f'获取tips提示文案为：{tips}')
            return tips

        except Exception as e:
            log.error(f'获取删除商品tips提示文案出现错误，异常为{e}')
            raise e


class ProductClassification(BasePage):
    """新增商品分类"""

    @allure.step('新增商品分类')
    def Add_product_sort(self, sortname):
        self.click_element(self.new_button)  # 点击新增
        # self.click_element(self.product_sort)  # 商品分类
        # self.click_element(self.new_button)  # 新增按钮
        self.input_text(self.Sort_Name_input, sortname)  # 输入分类名称
        self.click_element(self.Sort_Product_upload)  # 点击上传图片
        sleep(2)
        self.click_element(self.First_Product_Selector)  # 选择第一张图片
        sleep(2)
        self.click_element(self.Versify_Button)
        self.click_element(self.Outer_Vweify_Button)  # 点击外层的确认
        return ProductClassification()

    """搜索到第一个商品分类名称"""

    @allure.step('搜索到第一个商品分类名称')
    def get_first_product_sort_name(self, sortname):
        self.input_text(self.SearchSortName_input, sortname)
        self.click_element(self.Search_button)
        sleep(3)
        return self.get_element_text(self.Get_first_productsort)

    """删除搜索到的商品分类"""

    @allure.step('删除搜索到的商品分类')
    def Delect_product_sort(self):
        self.click_element(self.block_Verify_Button)  # 点击方块
        self.click_element(self.Delete_Button)  # 点击删除
        self.click_element(self.Verify_button)  # 点击确认
        return ProductClassification()

    """获取删除tips"""

    @allure.step('获取Tips提示消息')
    def Get_Tips(self):
        return self.get_element_text(self.get_tips)  # 获取删除成功tips


"""新增商品规格"""


class Specification(BasePage):
    @allure.step('添加商品规格')
    def add_Specification(self, Title_Name):
        self.click_element(self.new_button)
        self.input_text(self.Title_Name_input, Title_Name, desc="新增商品规格标题")  # 规格标题名称
        self.click_element(self.add_unit)  # 点击添加新规格
        self.input_text(self.nuit_input, generate_random_unit(), desc="新增商品规格")  # 规格
        self.input_text(self.nuitValue_input1, f"1{generate_random_unit()}", desc="新增商品规格值1")
        sleep(0.5)
        self.click_element(self.verify_button, desc='点击确认')
        sleep(1)
        self.input_text(self.nuitValue_input2, f"2{generate_random_unit()}", desc="新增商品规格值2")
        sleep(0.5)
        self.click_element(self.Add_button)  # 点击添加
        sleep(0.5)
        self.input_text(self.nuitValue_input3, f"3{generate_random_unit()}", desc="新增商品规格值3")
        self.click_element(self.Verify_button, desc='点击确认')
        sleep(4)
        return Specification()

    @allure.step('获取第一个规格名称')
    def get_first_nuit_name(self):
        sleep(1)
        return self.get_element_text(self.Unit_name)

    @allure.step('删除搜索到的规格')
    def Delete_nuit(self):
        sleep(3)
        self.click_element(self.Unit_block, desc="点击方块")
        self.click_element(self.Delete_button, desc="点击删除按钮")
        self.click_element(self.verify_button2, desc="点击确认")
        return Specification()

    @allure.step('获取删除规格的Tips提示')
    def Get_Tips(self):
        return self.get_element_text(self.get_tips)  # 获取删除成功tips


"""运费模版"""


class FreightTemplate(BasePage):
    @allure.step('添加运费模版')
    def add_FreightTemplate(self, Temple_name):
        self.click_element(self.new_button, desc="点击新增")
        self.input_text(self.Temple_name_input, Temple_name, desc="输入运费模版名称")
        self.click_element(self.Appoint_postsman, desc="选择指定包邮")
        self.click_element(self.Alone_Appoint_Postsman, desc="点击单独包邮")
        self.click_element(self.Empty, desc='清空')
        sleep(1)
        self.click_element(self.All_Select, desc='全选')
        sleep(2)
        self.click_element(self.Verify_button1, desc='点击确认')
        sleep(2)
        self.input_text(self.Number_of_packets_input, 1, desc='包邮件数')
        self.input_text(self.Amount_of_packets_input, 0, desc='包邮金额')
        self.input_text(self.Sort, 1, desc='输入排序的序列')
        self.click_element(self.Submit_now, desc='点击立即提交')
        sleep(2)
        return FreightTemplate()

    @allure.step('获取第一个运费模版')
    def get_first_Temple_name(self):
        sleep(3)
        return self.new_get_elelment_attribute(self.First_temple_name, desc='获取元素属性的值')

    @allure.step('删除运费模版')
    def Delete_TempleName(self):
        sleep(2)
        self.click_element(self.block_button, desc='点击勾选框')
        self.click_element(self.Delete_button, desc="点击删除按钮")
        sleep(1)
        self.click_element(self.verify_button2, desc="点击确认")
        sleep(1)
        return FreightTemplate()

    @allure.step('获取删除运费模版Tips提示')
    def Get_Tips(self):
        return self.get_element_text(self.get_tips)  # 获取删除成功tips


if __name__ == '__main__':
    test_flag = 1
    if test_flag == 1:
        # 第一次运行 多个key ，多拿了页面
        # 第二次运行 只有本页面了，
        # 第三次运行， 元素名成了类的属性
        loginpage = Specification()
        from objprint import op

        op(loginpage)
